package com.dsdl.chatapp.data.source.local;

import android.content.ContentValues;
import android.content.Context;
import android.support.annotation.NonNull;

import com.dsdl.chatapp.data.SocketMessage;
import com.dsdl.chatapp.data.source.ChatDataSource;
import com.dsdl.chatapp.common.db.ChatDAO;
import com.dsdl.chatapp.common.db.IChatDAO;

/**
 * Created by zzg on 2016/8/26.
 * 本地存储消息
 */
public class ChatLocalDataSource implements ChatDataSource {
    private static ChatLocalDataSource INSTANCE;
    private IChatDAO mIChatDAO;

    private ChatLocalDataSource(@NonNull Context context){
        mIChatDAO = ChatDAO.getInstance();
    }

    public static ChatLocalDataSource getInstance(@NonNull Context context){
        if (INSTANCE == null){
            INSTANCE = new ChatLocalDataSource(context);
        }
        return INSTANCE;
    }

    @Override
    public void saveMsg(@NonNull SocketMessage chatMsg) {
        ContentValues values = new ContentValues();
        values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_FROM_USER,chatMsg.getFromUser());
        values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_TO_USER,chatMsg.getToUser());
        values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_CREATE_TIME,chatMsg.getCreateTime());
        values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_CONTENT,chatMsg.getContent());
        values.put(ChatPersistenceContract.ChatEntry.COLUMN_NAME_MSG_TYPE,chatMsg.getMsgType());
        mIChatDAO.insert(values);
    }

    @Override
    public void deleteMsg(@NonNull SocketMessage chatMsg) {
        mIChatDAO.deleteById(chatMsg.get_id());
    }

    @Override
    public void deleteAllMsg(String userId) {
        mIChatDAO.deleteAllByUserId(userId);
    }

    @Override
    public void loadOffLineMsg(String userId) {

    }
}
